import requests
import json
import math
from liebiaowang import get_company_info

host_url = "http://127.0.0.1:8008"
def get_dance_company_detail():
    '''
        获取培训班的url, 获取培训班详情, 并插入数据库
    '''
    # 01 获取培训班url地址, state=1 草稿 / state=3 异常 / state=2 成功
    url = "{}/dance_companys/?state=1".format(host_url)
    headers = { 'cache-control': "no-cache"}
    res = requests.request("GET", url, headers=headers)
    res = json.loads(res.text)
    total_count = res.get('count')
    page_size = 100
    page_count = math.ceil(total_count/page_size)

    for page_num in range(1, page_count+1):
        request_url = "{}/dance_companys/?page_size={}&page={}&state=1".format(host_url, page_size, page_num)
        tmp_res = requests.request("GET", request_url, headers=headers)
        tmp_res = json.loads(tmp_res.text)
        tmp_results = tmp_res.get('results')
        # 02 通过详情url, 获取详细信息
        for company in tmp_results:
            company_info = {}
            company_info['company_url'] = company["company_url"]
            company_info['id'] = company["id"]
            try:
                tmp_company_info = get_company_info(company_url=company['company_url'])
                company_info['state'] = 2
                company_info.update(tmp_company_info)
            except Exception as e:
                print("异常信息: {}, {}".format(company['company_url'], str(e)))
                company_info['state'] = 3

            # 04 把获取到的详情页的url存储到数据库
            update_company_info(company_data=company_info)

def update_company_info(company_data):
    """
    :param company_data:  { "id": 1,
                            "title": "22222",
                            "company_url": "http://baidu.com",
                            "lianxiren": "张三",
                            "phone" : "10086",
                            "renzheng": "xxx已认证,xxx未认证",
                            "desc": "详情描述",
                            "state": 1}
    DOING_STATE = (
    (1, "草稿"),
    (2, "成功"),
    (3, "异常"),
    """
    # 注意restful风格, 连接地址如:  http://127.0.0.1:8008/dance_companys/1/
    url = "{}/dance_companys/{}/".format(host_url, company_data['id'])
    company_data = json.dumps(company_data)  # 把字典转成json格式
    headers = {
        'Content-Type': "application/json",
        'cache-control': "no-cache",
    }

    response = requests.request("PUT", url, data=company_data, headers=headers)
    print("company_data: ", company_data)
    print("培训班详情数据已更新: ", response.text)


if __name__ == '__main__':
    get_dance_company_detail()



